package com.px.user.controller;

import com.px.common.core.Response;
import com.px.user.entity.Sensi;
import com.px.user.mapper.SensiMapper;
import com.px.user.model.ActionModel;
import com.px.user.model.PagingResp;
import com.px.user.service.SensiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Authorization;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import java.sql.Timestamp;

@Slf4j
@Validated
@RestController
@RequestMapping("/sensi")
@Api(value = "敏感词管理", tags = {"敏感词管理"})
public class SensiController {

    @Autowired
    private SensiMapper sensiMapper;

    @Autowired
    private SensiService sensiService;

    @PostMapping
    @PreAuthorize("hasAnyAuthority('ADMIN','CHILDADMIN')")
    @ApiOperation(value = "敏感词创建", authorizations = {@Authorization(value = "token")}, tags = {"敏感词", "总管"})
    public Response sensiPost(@Validated(ActionModel.Insert.class) @RequestBody Sensi sensi) {
        sensiService.sensiInsert(sensi);
        return Response.success(null);
    }

    @PutMapping
    @PreAuthorize("hasAnyAuthority('ADMIN','CHILDADMIN')")
    @ApiOperation(value = "敏感词更新", authorizations = {@Authorization(value = "token")}, tags = {"敏感词", "总管"})
    public Response sensiPut(@Validated(ActionModel.Update.class) @RequestBody Sensi sensi) {
        sensiService.sensiUpdate(sensi);
        return Response.success(null);
    }

    @GetMapping
    @PreAuthorize("hasAnyAuthority('ADMIN','CHILDADMIN')")
    @ApiOperation(value = "敏感词搜索", authorizations = {@Authorization(value = "token")}, tags = {"敏感词", "总管"})
    public Response<PagingResp<Sensi>> sensiGet(
            @ApiParam(value = "用户ID") @RequestParam(value = "id", required = false) Integer id,
            @ApiParam(value = "开始时间", example = "1566805792293") @RequestParam(value = "qstart", required = false) Long qstart,
            @ApiParam(value = "截止时间") @RequestParam(value = "qend", required = false) Long qend,
            @ApiParam(value = "敏感词(模糊匹配)", example = "饱饱") @RequestParam(value = "txt", required = false) String txt,
            @ApiParam(value = "排序字段(倒序)", example = "updateTime") @RequestParam(value = "orderField", required = false, defaultValue = "createTime") String orderField,
            @ApiParam(value = "页码", example = "0") @RequestParam(value = "page", required = false, defaultValue = "0") @Min(0) Integer page,
            @ApiParam(value = "返回数量", example = "10") @RequestParam(value = "limit", required = false, defaultValue = "10") @Min(1) Integer limit) {
        Sensi qsensi = Sensi.builder()
                .id(id)
                .txt(txt)
                .qstart(qstart == null ? null : new Timestamp(qstart))
                .qend(qend == null ? null : new Timestamp(qend))
                .orderField(orderField)
                .startIndex(page * limit)
                .limit(limit)
                .build();

        return Response.success(PagingResp.<Sensi>builder()
                .itemCount(sensiMapper.pagingCount(qsensi))
                .items(sensiMapper.paging(qsensi))
                .build());
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('ADMIN','CHILDADMIN')")
    @ApiOperation(value = "敏感词删除", authorizations = {@Authorization(value = "token")}, tags = {"敏感词", "总管"})
    public Response sensiDelete(@PathVariable("id") Integer id) {
        sensiMapper.delete(id);
        return Response.success(null);
    }

    @GetMapping("/check")
    @ApiOperation(value = "敏感词检查", tags = {"敏感词"})
    public Response sensiCheckGet(@ApiParam(value = "敏感词") @RequestParam("txt") String txt){
        return Response.success(sensiService.sensiFilter(txt));
    }
}
